Skip to content

dns: fix crash on setServers with port 0 - #65009

Open
lazerg wants to merge 2 commits into
nodejs:mainfrom
lazerg:fix/issue-65006-setservers-port-zero
Open

dns: fix crash on setServers with port 0#65009
lazerg wants to merge 2 commits into
nodejs:mainfrom
lazerg:fix/issue-65006-setservers-port-zero

Conversation

@lazerg

@lazerg lazerg commented Aug 4, 2026

Copy link
Copy Markdown

dns.setServers(['1.1.1.1:0']) aborts the process:

Assertion failed: portValue->Int32Value(env->context()).FromJust()

SetServers() in src/cares_wrap.cc checked the port with CHECK(portValue->Int32Value(env->context()).FromJust()). That asserts the port value is non-zero, not that the conversion succeeded, so a port of 0 trips it. The family value had the same problem. Both now use Maybe::To(), like the rest of the file.

c-ares already treats port 0 as "use the default port", so 1.1.1.1:0 becomes 1.1.1.1:53. [::1]:0 behaved that way already, since the bracketed-IPv6 branch in lib/internal/dns/utils.js coerces 0 to 53 before it ever reaches C++.

Fixes: #65006

Signed-off-by: Lazizbek Ergashev <lazerg2@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. cares Issues and PRs related to the c-ares dependency or the cares_wrap binding. needs-ci PRs that need a full CI run. labels Aug 4, 2026
Comment thread src/cares_wrap.cc

@Renegade334 Renegade334 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Note that this preserves the previous behaviour of crashing the process if the port was greater than INT_MAX. Indeed, even below that point, any port value greater than 65535 overflows:

> dns.setServers(['1.1.1.1:65535', '1.1.1.1:65536', '1.1.1.1:65537'])
> dns.getServers()
[ '1.1.1.1:65535', '1.1.1.1', '1.1.1.1:1' ]

If you're happy, I think it would be a worthwhile addition to add validation to the port values, either as part of this PR or as a follow-up PR?

@lazerg

lazerg commented Aug 4, 2026

Copy link
Copy Markdown
Author

Thanks for the review. I couldn't reproduce the wraparound though — on this branch (c-ares 1.34.8, bundled, untouched by this diff):

> dns.setServers(['1.1.1.1:65535', '1.1.1.1:65536', '1.1.1.1:65537'])
Uncaught Error [ERR_DNS_SET_SERVERS_FAILED]: c-ares failed to set servers: "Misformatted string" [...]

ares_parse_port() (deps/cares/src/lib/str/ares_str.c) already caps at 65535 via ares_str_parse_uint(str, 65535UL, &val), so the CSV parse fails and the whole call throws, restoring the previous server list. That check is unrelated to this PR's diff, so it should behave the same on main.

Might be version-dependent on your end. Since I can't reproduce silent corruption on the current bundled c-ares, I'd rather not scope-creep this PR with validatePort() unless you still want the earlier/clearer JS-side error as its own improvement — happy to open that as a follow-up if so.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 4, 2026
@Renegade334 Renegade334 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Aug 4, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 4, 2026
@nodejs-github-bot

This comment was marked as outdated.

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.29%. Comparing base (ece8bbe) to head (42c89ee).
⚠️ Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
src/cares_wrap.cc 50.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65009      +/-   ##
==========================================
- Coverage   90.29%   90.29%   -0.01%     
==========================================
  Files         762      759       -3     
  Lines      247646   247605      -41     
  Branches    46709    46672      -37     
==========================================
- Hits       223619   223564      -55     
- Misses      15485    15516      +31     
+ Partials     8542     8525      -17     
Files with missing lines Coverage Δ
src/cares_wrap.cc 62.93% <50.00%> (ø)

... and 54 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. cares Issues and PRs related to the c-ares dependency or the cares_wrap binding. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dns.setServers.apply triggers SIGABRT when passed an address with port 0

4 participants